//
//  TMGChangePwdViewController.m
//  TMGold
//
//  Created by Mac on 2017/12/8.
//  Copyright © 2017年 tangmu. All rights reserved.
//

#import "TMGChangePwdViewController.h"
#import "UITextField+STInputLimit.h"
#import "TMGResetPwdViewController.h"
#import "TMGACResetPwdViewController.h"
@interface TMGChangePwdViewController ()
@property(nonatomic, strong) UITextField                     *oldPwdTextField;
@property(nonatomic, strong) UITextField                     *nowPwdTextField;
@property(nonatomic, strong) UITextField                     *confimPwdTextField;
@property(nonatomic, strong) NSArray<STTableEasyModel*>                     *dataSource;
@end

@implementation TMGChangePwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"修改登录密码";
    [self configSubView];
    [self addNotifacations];
    [self configFooterView];
    [self st_setRightItemWithTitle:@"重置" titleColor:[UIColor whiteColor]];
    // Do any additional setup after loading the view.
}
- (void)addNotifacations{
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChange:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    
}
- (void)UITextFieldTextDidChange:(NSNotification*)notify{
    if (notify.object == self.oldPwdTextField) {
        [self.oldPwdTextField st_textInputLimitWithLength:16];
    }
    if (notify.object == self.nowPwdTextField) {
        [self.nowPwdTextField st_textInputLimitWithLength:16];
    }
    if (notify.object == self.confimPwdTextField) {
        [self.confimPwdTextField st_textInputLimitWithLength:16];
    }
}
- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
#pragma mark --subView
- (void)configSubView{
    self.oldPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.oldPwdTextField.placeholder = @"请输入原密码";
    
    self.nowPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.nowPwdTextField.placeholder = @"请输入6~16位的密码";
    
    self.confimPwdTextField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth * 0.66, 20)];
    self.confimPwdTextField.placeholder = @"请确认新密码";
    
    self.dataSource = @[
                        [[STTableEasyModel alloc] initWithTextString:@"原密码" accessoryView:self.oldPwdTextField],
                        [[STTableEasyModel alloc] initWithTextString:@"新密码" accessoryView:self.nowPwdTextField],
                        [[STTableEasyModel alloc] initWithTextString:@"确认新密码" accessoryView:self.confimPwdTextField]];
    [self.tableView reloadData];
    
}
- (void)configFooterView{
    __weak typeof(self) weakSelf =  self;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    STLabel * titleLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, 0, 200, 44)
                                                     text:@"新密码不能和原密码相同"
                                                textColor:TM_secendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [footer addSubview:titleLable];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, 0, UIScreenWidth - 60 , 44)
                                                        title:@"确认修改"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:10
                                              backgroundColor:TM_orangeColor
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelectedLogoutButton];
    }];
    confimButton.st_centerY = 100;

    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    footer.height = confimButton.bottom + 20;
    self.tableView.tableFooterView = footer;
}
- (void)onSelectedLogoutButton{
    if (!self.oldPwdTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入原密码"];
        return ;
    }
    if (!self.nowPwdTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请输入新密码"];
        return ;
    }
    if (!self.confimPwdTextField.text.length) {
        [SVProgressHUD showErrorWithStatus:@"请再次输入密码"];
        return ;
    }
    if (self.nowPwdTextField.text.length < 6) {
        [SVProgressHUD showErrorWithStatus:@"密码不能小于6位"];
        return ;
    }
    if (![self.confimPwdTextField.text isEqualToString:self.nowPwdTextField.text]) {
        [SVProgressHUD showErrorWithStatus:@"两次密码输入不一致"];
        return ;
    }
    [self sendChangePWdRequest];
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1 ;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSource.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 44;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifier];
        
    }
    cell.textLabel.textColor = UIColorFromRGBA(0x333333);
    cell.textLabel.font = [UIFont systemFontOfSize:14];
    cell.detailTextLabel.font = [UIFont systemFontOfSize:13];
    cell.detailTextLabel.textColor = UIColorFromRGBA(0x999999);
    
    STTableEasyModel * model = self.dataSource[indexPath.row];
    cell.textLabel.text = model.textString;
    cell.detailTextLabel.text = model.detailString;
    if (model.accessoryView) {
        cell.accessoryView = model.accessoryView;
    }else{
        cell.accessoryType = model.accessoryType;
    }
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)st_rightBarAction:(id)sender{
    [self.navigationController pushViewController:[TMGACResetPwdViewController new] animated:YES];
}
- (void)sendChangePWdRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@",service_header,@"/gold/public/api/personal/editPassword"];
    if ([STUserManger defult].userID.length) {
        [dic setObject:[STUserManger defult].userID forKey:@"uid"];
    }
    if ([STUserManger defult].token.length) {
        [dic setObject:[STUserManger defult].token forKey:@"token"];
    }
    if (self.oldPwdTextField.text.length) {
        [dic setObject:self.oldPwdTextField.text forKey:@"old_pass"];
    }
    if (self.nowPwdTextField.text.length) {
        [dic setObject:self.nowPwdTextField.text forKey:@"new_pass1"];
    }
    if (self.confimPwdTextField.text.length) {
        [dic setObject:self.confimPwdTextField.text forKey:@"new_pass2"];
    }
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showSuccessWithStatus:@"修改密码成功"];
                                                 [self.navigationController popViewControllerAnimated:YES];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end

